package com.opennetexam.manager.bussinessservice;

import com.opennetexam.manager.dto.ExaminerPhotoDTO;
import com.opennetexam.manager.databasemodel.ExamPhoto;
import com.opennetexam.manager.mapper.ExamPhotoMapper;
import com.opennetexam.manager.common.OperationResult;
import com.opennetexam.manager.enums.OperationResultType;
import com.opennetexam.manager.enums.PhotoResourceType;
import com.opennetexam.manager.utils.PublicHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by admin on 2017/11/22.
 */
@Service("examPhotoService")
@Transactional(rollbackFor = RuntimeException.class)
public class ExamPhotoService {

    @Autowired
    private ExamPhotoMapper examPhotoMapper;

    @Value("${FaceCompareSocre}")
    private Double faceCompareSocre;

    /**
     * 获取考生照片列表
     * @param planId  计划标识
     * @param roomId  考场标识
     * @param sceneId  场次标识
     * @param statusId  拍照状态  null:全部  0：未拍照  1：已拍照
     * @param appId  主办方标识
     * @param examineeName  考生姓名
     * @param examCardNo  准考证号
     * @return
     */
    public OperationResult getPhotoList(String planId, String roomId, String sceneId, Integer statusId, String examineeName, String examCardNo, String appId){
        OperationResult operationResult = new OperationResult();
        OperationResultType resultType = OperationResultType.Success;
        String message = "获取考生照片列表成功!";
        List<Map> photoList = examPhotoMapper.getPhotoList(planId,roomId,sceneId,examineeName,examCardNo,appId,statusId);
        operationResult.setResultType(resultType);
        operationResult.setMessage(message);
        operationResult.setAppendData(photoList);
        return operationResult;
    }

    /**
     * 根据照片资源类型获取考生照片列表
     * @param examinerArrangeID
     * @return
     */
    public Map<String, List<ExaminerPhotoDTO>> getExaminerPhotoByType(String examinerArrangeID){
        Map<String, List<ExaminerPhotoDTO>> returnMap = new HashMap<>();
        List<ExaminerPhotoDTO> examinerPhotoByType = examPhotoMapper.getExaminerPhotoByType(examinerArrangeID);
        if(examinerPhotoByType.isEmpty()){
            returnMap.put(PhotoResourceType.ExaminerHeadPhoto.getPrefix().toString(), Arrays.asList(examPhotoMapper.getPhotoHeader(examinerArrangeID)));
        }else{
            returnMap = examinerPhotoByType.stream().sorted(Comparator.comparing(ExaminerPhotoDTO::getInsertTime).reversed()).collect(Collectors.groupingBy(ExaminerPhotoDTO::getPhotoResourceType, Collectors.toList()));
        }
        return returnMap;
    }


    /**
     * 保存考生照片
     * @param examPhoto
     */
    public void saveExamineePhoto(ExamPhoto examPhoto) {
        examPhotoMapper.saveExamineePhoto(examPhoto);
    }

    /**
     * 查询考生拍照
     * @param examineeArrangeID
     * @param photoResourceType
     * @return
     */
    public List<ExamPhoto> getExamPhotoByWhere(String examineeArrangeID, Integer photoResourceType) {
        return examPhotoMapper.getExamPhotoByWhere(examineeArrangeID, photoResourceType);
    }

    /**
     * 修改考生拍照
     * @param examPhoto
     */
    public void updateExamineePhoto(ExamPhoto examPhoto) {
        examPhotoMapper.updateExamineePhoto(examPhoto);
    }
}
